package controller.room
{
	import controller.BaseController;
	import controller.ControllerManager;
	import controller.game.GameController;
	import controller.game.GameObjectManager;	
	import controller.SocketManager;
	import controller.events.SocketEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.Label;
	import mx.core.FlexGlobals;
	
	import poker.LevelDefine;
	
	import spark.components.Group;
	import spark.components.Image;
	
	public class RoomController extends BaseController
	{
		// static instance 
		public static var instance:RoomController = null;
		
		public var userId:String;
		public var roomid:String;
		public var pos:int;
		public function RoomController()
		{
			super();
			if ( instance != null )
				throw new Error( "Only one Singleton instance should be instantiated" ); 
		}
		static public function get Instance():RoomController
		{
			if ( instance == null )
				instance = new RoomController();
			return instance;
		}	
		//加入房间
		public function GAME_ROOM_JOIN(roomid:String,pos:String):void
		{
			//roomid~pos
			var p:int = -1;				
			if(pos == "left_"+roomid)
			{
				p = 1;
			}
			else if(pos == "down_"+roomid)
			{
				p = 2;
			}
			else if(pos == "right_"+roomid)
			{
				p = 3;
			}
			
			if(roomid.indexOf("grouptable_")!=-1)
			{
				roomid=roomid.replace("grouptable_","");
			}
			initEvents();
			SocketManager.Instance.addEventListener(SocketEvent.GAME_ROOM_JOIN,GAME_ROOM_JOIN_HANDLER);
			SocketManager.Instance.send(this.CreateOutMessage(BaseController.FIGHT_LANDLORD,"GAME_ROOM_JOIN",roomid+"~"+p));
			
		}
		//加入房间
		public function GAME_ROOM_EXIT():void
		{
			//roomid~pos
			SocketManager.Instance.addEventListener(SocketEvent.GAME_ROOM_EXIT,GAME_ROOM_EXIT_HANDLER);
			SocketManager.Instance.send(this.CreateOutMessage(BaseController.FIGHT_LANDLORD,"GAME_ROOM_EXIT",""));
		}
		
		//加入房间
		public function GAME_ROOM_READY():void
		{
			//roomid~pos
			SocketManager.Instance.addEventListener(SocketEvent.GAME_ROOM_READY,GAME_ROOM_READY_HANDLER);
			SocketManager.Instance.send(this.CreateOutMessage(BaseController.FIGHT_LANDLORD,"GAME_ROOM_READY",""));
			GameObjectManager.Instance.removeCardfromScreen("HandCardDown");
			GameObjectManager.Instance.removeCardfromScreen("PlayedCardDown");
			
		}
		public function initEvents():void{
			
			SocketManager.Instance.addEventListener(SocketEvent.GAME_ROOM_PLAYER_EXIT,GAME_ROOM_PLAYER_EXIT_HANDLER);
			SocketManager.Instance.addEventListener(SocketEvent.GAME_ROOM_PLAYER_JOIN,GAME_ROOM_PLAYER_JOIN_HANDLER);
			SocketManager.Instance.addEventListener(SocketEvent.GAME_ROOM_PLAYER_READY,GAME_ROOM_PLAYER_READY_HANDLER);
			GameController.instance.initEvents();
			
		}
		public function removeEvents():void{
			SocketManager.Instance.removeEventListener(SocketEvent.GAME_ROOM_PLAYER_EXIT,GAME_ROOM_PLAYER_EXIT_HANDLER);
			SocketManager.Instance.removeEventListener(SocketEvent.GAME_ROOM_PLAYER_JOIN,GAME_ROOM_PLAYER_JOIN_HANDLER);
			SocketManager.Instance.removeEventListener(SocketEvent.GAME_ROOM_PLAYER_READY,GAME_ROOM_PLAYER_READY_HANDLER);
			
		}
		private function GAME_ROOM_PLAYER_READY_HANDLER(event:SocketEvent):void{
			trace("接收到派发消息：GAME_ROOM_PLAYER_READY_HANDLER"+event.results[1]);
			controller.ControllerManager.Instance.debug("接收到派发消息：GAME_ROOM_PLAYER_READY_HANDLER"+event.results[3]);
			if(this.receive(event.results)){	
	
				if(this.result.pos==this.getplayerpos(true)){
					GameObjectManager.Instance.removeCardfromScreen("HandCardLeft");
					GameObjectManager.Instance.removeCardfromScreen("PlayedCardLeft");
				}
				if(this.result.pos==this.getplayerpos(false)){
					GameObjectManager.Instance.removeCardfromScreen("HandCardRight");			
					GameObjectManager.Instance.removeCardfromScreen("PlayedCardRight");
				}
				updatePlayerReadyByPos(this.result,true);				
			}
		}
		private function GAME_ROOM_PLAYER_JOIN_HANDLER(event:SocketEvent):void{
			trace("接收到派发消息：GAME_ROOM_PLAYER_JOIN_HANDLER"+event.results[1]);
			controller.ControllerManager.Instance.debug("接收到派发消息：GAME_ROOM_PLAYER_JOIN_HANDLER"+event.results[3]);
			if(this.receive(event.results)){
				updatePlayerJoinExitByPos(this.result,true);				
			}
			GameController.Instance.initEvents();
		}
		private function GAME_ROOM_PLAYER_EXIT_HANDLER(event:SocketEvent):void{
			trace("接收到派发消息：GAME_ROOM_PLAYER_EXIT_HANDLER"+event.results[1]);
			controller.ControllerManager.Instance.debug("接收到派发消息：GAME_ROOM_PLAYER_EXIT_HANDLER"+event.results[3]);
			if(this.receive(event.results)){
				updatePlayerJoinExitByPos(this.result,false);				
			}
		}
		private function GAME_ROOM_JOIN_HANDLER(event:SocketEvent):void
		{
			SocketManager.Instance.removeEventListener(SocketEvent.GAME_ROOM_JOIN,GAME_ROOM_JOIN_HANDLER);
			trace("接收到派发消息：GAME_ROOM_JOIN_HANDLER"+event.results[1]);
			if(this.receive(event.results)){
				ControllerManager.Instance.state=2;
				this.roomid=this.result.rid;
				this.pos=this.result.pos;
				FlexGlobals.topLevelApplication.currentState = "Game";
				initEvents();
				//放弃提示
				FlexGlobals.topLevelApplication.imgDiscardLeft.visible=false;
				FlexGlobals.topLevelApplication.imgDiscardDown.visible=false;
				FlexGlobals.topLevelApplication.imgDiscardRight.visible=false;
				//出牌
				FlexGlobals.topLevelApplication.label_thinking.visible=false;
				//出牌三按钮
				FlexGlobals.topLevelApplication.GameCardsCommand.visible=false;
				//叫分三按钮
				FlexGlobals.topLevelApplication.GameScoreCommand.visible=false;
				//闹钟
				FlexGlobals.topLevelApplication.ClockTimeInfo.visible=false;
				//准备按钮
				FlexGlobals.topLevelApplication.btnReady.visible = true;
				//准备完毕
				FlexGlobals.topLevelApplication.imgPlayerLeftReady.visible=false;
				FlexGlobals.topLevelApplication.imgPlayerRightReady.visible=false;
				FlexGlobals.topLevelApplication.imgPlayerDownReady.visible=false;				
			
				//剩余牌数提示隐藏
				FlexGlobals.topLevelApplication.Label_leftcardsnumDownl.visible=false;
				FlexGlobals.topLevelApplication.Label_leftcardsnumDown.visible=false;
				FlexGlobals.topLevelApplication.Label_leftcardsnumRightl.visible=false;
				FlexGlobals.topLevelApplication.Label_leftcardsnumRight.visible=false;
				FlexGlobals.topLevelApplication.Label_leftcardsnumLeft.visible=false;
				FlexGlobals.topLevelApplication.Label_leftcardsnumLeftl.visible=false;
				updatePlayerInfo(this.result);
				updatePlayerReadyState(this.result);
			}
		}
		
		private function GAME_ROOM_EXIT_HANDLER(event:SocketEvent):void
		{
			trace("接收到派发消息：GAME_ROOM_EXIT_HANDLER"+event.results[1]);
			if(this.receive(event.results)){
				SocketManager.Instance.removeEventListener(SocketEvent.GAME_ROOM_EXIT,GAME_ROOM_EXIT_HANDLER);
			   FlexGlobals.topLevelApplication.currentState = "GameLobby";
			   removeEvents();
			   ControllerManager.Instance.state=0;
			   this.roomid="";
			   this.pos=0;
			   GameController.Instance.removeEventsWhenExitRoom();			   
			}
		}	
		private function GAME_ROOM_READY_HANDLER(event:SocketEvent):void
		{
			SocketManager.Instance.removeEventListener(SocketEvent.GAME_ROOM_READY,GAME_ROOM_READY_HANDLER);
			trace("接收到派发消息：GAME_ROOM_READY_HANDLER"+event.results[1]);
			if(this.receive(event.results)){
			
				ControllerManager.Instance.state=3;
				FlexGlobals.topLevelApplication.imgPlayerDownReady.visible=true;
				FlexGlobals.topLevelApplication.btnReady.visible=false;
				if(this.result.pos==this.getplayerpos(true)){
					GameObjectManager.Instance.removeCardfromScreen("HandCardLeft");
					GameObjectManager.Instance.removeCardfromScreen("PlayedCardLeft");
				}
				if(this.result.pos==this.getplayerpos(false)){
				GameObjectManager.Instance.removeCardfromScreen("HandCardRight");			
				GameObjectManager.Instance.removeCardfromScreen("PlayedCardRight");
				}
			}
		}			
		//收到玩家准备消息，更新房间画面
		public function updatePlayerReadyByPos(obj:Object,isjoin:Boolean):void{
			var playerPos=obj.pos;
			var leftpos:int=getplayerpos(true);
			var rightpos:int=getplayerpos(false);
			if(playerPos==leftpos){
				
				FlexGlobals.topLevelApplication.imgPlayerLeftReady.visible = true;			
			}
			else if(playerPos==rightpos){
				FlexGlobals.topLevelApplication.imgPlayerRightReady.visible = true;	
			}
		}
		//收到玩家加入，退出消息的时候，更新房间画面
		public function updatePlayerJoinExitByPos(obj:Object,isjoin:Boolean):void{
			var playerPos=obj.pos;
			var leftpos:int=getplayerpos(true);
			var rightpos:int=getplayerpos(false);
			//更新左边玩家信息
			if(playerPos==leftpos){
				if(isjoin){				
						//左边的信息
						FlexGlobals.topLevelApplication.Lable_playernameLeft.text = obj.name;
						FlexGlobals.topLevelApplication.Lable_playerLevelLeft.text = LevelDefine.getLevelName(obj.score);				
						FlexGlobals.topLevelApplication.img_playerLeftBG.source =obj.avatar;
						/*FlexGlobals.topLevelApplication.img_playerLeftBG.width=30;
						FlexGlobals.topLevelApplication.img_playerLeftBG.height=30;*/
						FlexGlobals.topLevelApplication.playerinfoLeft.visible=true;
						
						FlexGlobals.topLevelApplication.Label_leftcardsnumLeft.visible=false;
						FlexGlobals.topLevelApplication.Label_leftcardsnumLeftl.visible=false;
						if(obj.ready)
						{
							FlexGlobals.topLevelApplication.imgPlayerLeftReady.visible = true;							
						}
						else{
							FlexGlobals.topLevelApplication.imgPlayerLeftReady.visible = false;
						}	
				}else
				{
					FlexGlobals.topLevelApplication.playerinfoLeft.visible = false;		
					FlexGlobals.topLevelApplication.imgPlayerLeftReady.visible=false;				
				}
				
			}else if(playerPos==rightpos){
				
				if(isjoin){				
					//you边的信息
					FlexGlobals.topLevelApplication.Lable_playernameRight.text = obj.name;
					FlexGlobals.topLevelApplication.Lable_playerLevelRight.text = LevelDefine.getLevelName(obj.score);				
					FlexGlobals.topLevelApplication.img_playerRightBG.source =obj.avatar;
				/*	FlexGlobals.topLevelApplication.img_playerRightBG.width=30;
					FlexGlobals.topLevelApplication.img_playerRightBG.height=30;*/
					FlexGlobals.topLevelApplication.playerinfoRight.visible=true;
					
					FlexGlobals.topLevelApplication.Label_leftcardsnumRightl.visible=false;
					FlexGlobals.topLevelApplication.Label_leftcardsnumRight.visible=false;
					if(obj.ready)
					{
						FlexGlobals.topLevelApplication.imgPlayerRightReady.visible = true;							
					}
					else{
						FlexGlobals.topLevelApplication.imgPlayerRightReady.visible = false;
						
					}	
				}else
				{
					//玩家推出，清除所有显示内容
					FlexGlobals.topLevelApplication.playerinfoRight.visible = false;	
					FlexGlobals.topLevelApplication.imgPlayerRightReady.visible=false;
				}				
			}
		}
		// 更新玩家的信息  和玩家的头像图片
		// 同时对玩家数据进行更新（这个数据是用来在右边的数据列表上用的）
		public function updatePlayerInfo(obj:Object):void
		{			
			var leftpos:int=getplayerpos(true);
			var rightpos:int=getplayerpos(false);
			FlexGlobals.topLevelApplication.label_tableName.text="["+obj.rid+"号房]";
			// 玩家的姓名，显示在右上
			if(obj.hasOwnProperty("players"))
			{
				if(obj.players.hasOwnProperty(pos))
				{
					//自己的信息
					FlexGlobals.topLevelApplication.Lable_playernameDown.text = obj.players[pos].name;
					FlexGlobals.topLevelApplication.Lable_playerLevelDown.text = LevelDefine.getLevelName(obj.players[pos].score);				
					FlexGlobals.topLevelApplication.img_playerDownBG.source =obj.players[pos].avatar;
			/*		FlexGlobals.topLevelApplication.img_playerDownBG.width=30;
					FlexGlobals.topLevelApplication.img_playerDownBG.height=30;*/
					FlexGlobals.topLevelApplication.playerinfoDown.visible=true;
					if(obj.players[pos].ready)
					{
					FlexGlobals.topLevelApplication.imgPlayerDownReady.visible = true;
					
					}
					else{
					FlexGlobals.topLevelApplication.imgPlayerDownReady.visible = true;
					}
				}	
				
				if(obj.players.hasOwnProperty(leftpos))
				{
					//左边的信息
					FlexGlobals.topLevelApplication.Lable_playernameLeft.text = obj.players[leftpos].name;
					FlexGlobals.topLevelApplication.Lable_playerLevelLeft.text = LevelDefine.getLevelName(obj.players[leftpos].score);				
					FlexGlobals.topLevelApplication.img_playerLeftBG.source =obj.players[leftpos].avatar;
				/*	FlexGlobals.topLevelApplication.img_playerLeftBG.width=30;
					FlexGlobals.topLevelApplication.img_playerLeftBG.height=30;*/
					FlexGlobals.topLevelApplication.playerinfoLeft.visible=true;
			       if(obj.players[leftpos].ready)
					{
					FlexGlobals.topLevelApplication.imgPlayerLeftReady.visible = true;
					
					}
					else{
					FlexGlobals.topLevelApplication.imgPlayerLeftReady.visible = true;
					}
				}
				else
				{
					FlexGlobals.topLevelApplication.playerinfoLeft.visible = false;	
				}
				
				if(obj.players.hasOwnProperty(rightpos))
				{
					//右边的信息
					FlexGlobals.topLevelApplication.Lable_playernameRight.text = obj.players[rightpos].name;
					FlexGlobals.topLevelApplication.Lable_playerLevelRight.text = LevelDefine.getLevelName(obj.players[rightpos].score);				
					FlexGlobals.topLevelApplication.img_playerRightBG.source =obj.players[rightpos].avatar;
					FlexGlobals.topLevelApplication.playerinfoRight.visible=true;
					if(obj.players[rightpos].ready)
					{
					FlexGlobals.topLevelApplication.imgPlayerRightReady.visible = true;
					
					}
					else{
					FlexGlobals.topLevelApplication.imgPlayerRightReady.visible = true;
					}
				}	
				else
				{
					FlexGlobals.topLevelApplication.playerinfoRight.visible = false;	
				}
			}
		}
		public function updatePlayerReadyState(obj:Object):void
		{		
			var leftpos:int=getplayerpos(true);
			var rightpos:int=getplayerpos(false);
			// 玩家的状态
			if(obj.hasOwnProperty("players"))
			{
				// self
				if(obj.players.hasOwnProperty(pos))
				{
					if(obj.players[pos].ready)
					{
						FlexGlobals.topLevelApplication.imgPlayerDownReady.visible = true;
						FlexGlobals.topLevelApplication.btnReady.visible = false;
					}
					else
					{
						FlexGlobals.topLevelApplication.imgPlayerDownReady.visible = false;
					}
				}
				else{
					FlexGlobals.topLevelApplication.imgPlayerDownReady.visible = false;
				}
				
				//right
				if(obj.players.hasOwnProperty(rightpos))
				{
					if(obj.players[rightpos].ready)
					{
						FlexGlobals.topLevelApplication.imgPlayerRightReady.visible = true;
					}
					else
					{
						FlexGlobals.topLevelApplication.imgPlayerRightReady.visible = false;
					}
				}
				else{
					FlexGlobals.topLevelApplication.imgPlayerRightReady.visible = false;
				}
				
				//left
				if(obj.players.hasOwnProperty(leftpos))
				{
					if(obj.players[leftpos].ready)
					{
						FlexGlobals.topLevelApplication.imgPlayerLeftReady.visible = true;
					}
					else
					{
						FlexGlobals.topLevelApplication.imgPlayerLeftReady.visible = false;
					}
				}
				else{
					FlexGlobals.topLevelApplication.imgPlayerLeftReady.visible = false;
				}
				
			}
		}		
		public function getplayerpos(isleft:Boolean):int{		
			if(isleft){
				if(pos==1)
				{
					return 3;
				}
				else
				{
					return (pos-1)%3;}
			}
			else
			{
				if(pos==1)
				{
					return 2;
				}
				else if(pos==2)
				{
					return 3;
				}
				else if(pos==3)
				{
					return 1;
				}
				else 
				{
					return 0;
				}
			}
		}
	}
}